package com.ethan.minimalist.ui.mine.withdraw

import android.app.Activity
import android.content.Intent
import android.text.Editable
import android.text.TextUtils
import android.text.TextWatcher
import android.widget.Toast
import com.cxz.kotlin.baselibs.base.BaseMvpActivity
import com.cxz.kotlin.baselibs.ext.setSingleClickListener
import com.cxz.kotlin.baselibs.ext.showToast
import com.cxz.kotlin.baselibs.http.constant.HttpConstant
import com.cxz.kotlin.baselibs.widget.CustomToast
import com.ethan.minimalist.R
import com.ethan.minimalist.bean.UserBean
import com.ethan.minimalist.constant.Constant
import com.ethan.minimalist.mvp.contract.IWithdrawContract
import com.ethan.minimalist.mvp.presenter.WithdrawPresenter
import com.google.gson.Gson
import com.gyf.immersionbar.ImmersionBar
import com.tencent.mmkv.MMKV
import kotlinx.android.synthetic.main.activity_withdraw_first_step.*

/**
 * @ClassName:      WithdrawFirstStepActivity
 * @Description:
 * @Author:         ethanWu
 * @CreateDate:     2020/3/19 9:55
 */
open class WithdrawFirstStepActivity :
    BaseMvpActivity<IWithdrawContract.View, IWithdrawContract.Presenter>(), IWithdrawContract.View {
    private var mUser: UserBean? = null
    private var isOk: Boolean? = null

    override fun createPresenter(): IWithdrawContract.Presenter {
        return WithdrawPresenter()
    }

    override fun attachLayoutRes(): Int {
        return R.layout.activity_withdraw_first_step
    }

    override fun initView() {
        super.initView()
        ImmersionBar.with(this@WithdrawFirstStepActivity)
            .statusBarView(top_view)
            .statusBarDarkFont(true, 0.2f)
            .init()
    }

    override fun initData() {
        super.initData()
        if (mPresenter == null) {
            mPresenter = createPresenter()
        }
        mPresenter?.createKitPwd()
        mUser =
            Gson().fromJson(MMKV.defaultMMKV().decodeString(Constant.USER), UserBean::class.java)

        if (mUser != null) {
            tv_withdraw_amount.text =
                if (TextUtils.isEmpty(mUser!!.balance)) "可提现金额0元" else "可提现金额${mUser!!.balance}元"
        }

        tv_all_in.setSingleClickListener {
            et_money.setText(mUser?.balance)
        }

        tv_next.setSingleClickListener {
            if (isOk == null || !isOk!!) {
                showToast("请输入提现账号/口令/提现金额")
                return@setSingleClickListener
            }

            if (mUser == null) {
                return@setSingleClickListener
            }

            val account = et_account.text.toString()
            val password = et_password.text.toString()
            /* if(account != mUser!!.userName && account != mUser!!.nickName && account != mUser!!.mobile) {
                 showToast("提现账号仅限当前用户名/昵称/手机号")
                 return@setSingleClickListener
             }*/
            val money = et_money.text.toString()
            try {
                if (money.toFloat() == 0F) {
                    showToast("无可提现金额")
                    return@setSingleClickListener
                }
               /* val leftMoney: Double =
                    mUser!!.balance.toFloat() - money.toFloat() - (money.toFloat() * 0.001)*/

                mPresenter?.kitMoney(password,money)
            } catch (e: java.lang.Exception) {

            }


        }

       /* val currentTimeMillis = System.currentTimeMillis().toString()
        val digest = Md5Utils.digest(currentTimeMillis)
        val timeMills =
            currentTimeMillis.substring(currentTimeMillis.length - 5, currentTimeMillis.length)
        val digestStr = digest.substring(digest.length - 5, digest.length)
        tv_password.text = "$timeMills$digestStr"*/

        et_account.addTextChangedListener(object : TextWatcher {
            override fun afterTextChanged(s: Editable?) {
                isOk = !TextUtils.isEmpty(et_money.text.toString()) && !TextUtils.isEmpty(et_account.text.toString())
                        && !TextUtils.isEmpty(et_password.text.toString())
                setBackground()
            }

            override fun beforeTextChanged(s: CharSequence?, start: Int, count: Int, after: Int) {

            }

            override fun onTextChanged(s: CharSequence?, start: Int, before: Int, count: Int) {

            }
        })

        et_money.addTextChangedListener(object : TextWatcher {
            override fun afterTextChanged(s: Editable?) {
                val money = et_money.text.toString()
                isOk = !TextUtils.isEmpty(et_money.text.toString()) && !TextUtils.isEmpty(et_account.text.toString())
                        && !TextUtils.isEmpty(et_password.text.toString())
                setBackground()

                if (mUser == null) {
                    return
                }
                try {
                    if (money.toFloat() > mUser!!.balance.toFloat()) {
                        et_money.setText(mUser!!.balance)
                    }
                } catch (e: Exception) {

                }
            }

            override fun beforeTextChanged(s: CharSequence?, start: Int, count: Int, after: Int) {

            }

            override fun onTextChanged(s: CharSequence?, start: Int, before: Int, count: Int) {

            }
        })

        et_password.addTextChangedListener(object : TextWatcher {
            override fun afterTextChanged(s: Editable?) {
                isOk = !TextUtils.isEmpty(et_money.text.toString()) && !TextUtils.isEmpty(et_account.text.toString())
                        && !TextUtils.isEmpty(et_password.text.toString())
                setBackground()
            }

            override fun beforeTextChanged(s: CharSequence?, start: Int, count: Int, after: Int) {

            }

            override fun onTextChanged(s: CharSequence?, start: Int, before: Int, count: Int) {

            }
        })

    }

    private fun setBackground(): Unit {
        if (isOk == null) {
            return
        }
        tv_next.background =
            resources.getDrawable(if (isOk!!) R.drawable.bg_recharge else R.drawable.bg_next_normal)
    }

    override fun start() {

    }

    override fun withdrawSuccess(data: UserBean) {
        CustomToast(this, "提现申请中,请稍等",Toast.LENGTH_LONG).show()
        MMKV.defaultMMKV().encode(Constant.USER,Gson().toJson(data))
        MMKV.defaultMMKV().encode(HttpConstant.TOKEN_KEY,data?.token)
        showLoading()
        tv_next.postDelayed({
            hideLoading()
            startActivityForResult(Intent(this@WithdrawFirstStepActivity, WithdrawSecStepActivity::class.java),0x10)
        },500)


    }

    override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
        super.onActivityResult(requestCode, resultCode, data)
        if(requestCode == 0x10 && resultCode == Activity.RESULT_OK) {
            setResult(Activity.RESULT_OK)
            finish()
        }
    }
}